package com.xiaoxiao.springbootstudy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoxiao.springbootstudy.mapper.UserMapper;
import com.xiaoxiao.springbootstudy.mapper.UserRoleMapper;
import com.xiaoxiao.springbootstudy.pojo.User;
import com.xiaoxiao.springbootstudy.pojo.UserRole;
import com.xiaoxiao.springbootstudy.pojo.vo.CountResult;
import com.xiaoxiao.springbootstudy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author XiaoXiao
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2025-05-19 14:34:27
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Transactional
    @Override
    public void saveUserRolerService(User user) {
        userMapper.saveUserMapper(user);

        Integer userId = user.getId();
        Integer[] roleIds = user.getRids();
        for (Integer roleId : roleIds) {
            UserRole userRole = new UserRole();
            userRole.setUid(userId);
            userRole.setRid(roleId);
            userRoleMapper.insert(userRole);
        }
    }

    @Override
    public Map<String, Object> getUserRolerService(Integer pageNum, Integer pageSize) {
        Map<String, Object> result = new HashMap<String, Object>();
        Page<User> page = new Page<>(pageNum, pageSize);
        List<User> users = userMapper.selectList(page, null);

        result.put("total", page.getTotal());
        result.put("list", users);
        return result;

    }

    @Transactional
    @Override
    public void updateUserRoleService(User user) {


        //删除当前更新用户和角色的所有关系，断开关系
        QueryWrapper<UserRole> del = new QueryWrapper<>();
        del.eq("uid", user.getId());  //where uid=?
        userRoleMapper.delete(del);

        //更新用户信息
        userMapper.updateById(user);

        //中间关系表重新添加数据
        Integer[] rids = user.getRids();
        for (Integer rid : rids) {
            UserRole ur = new UserRole();
            ur.setUid(user.getId());
            ur.setRid(rid);
            userRoleMapper.insert(ur);
        }
    }

    @Transactional
    @Override
    public void deleteUserRoleService(Integer id) {
        //删除用户
        userMapper.deleteById(id);

        QueryWrapper<UserRole> wrapper=new QueryWrapper<>();
        wrapper.eq("uid",id);
        //删除用户角色信息
        userRoleMapper.delete(wrapper);
    }

    @Override
    public List<CountResult> countEmployeeAageService() {
        return userMapper.countEmployeeAgeMapper();
    }

    @Override
    public List<CountResult> countEmployeeEduService() {
        return userMapper.countEmployeeEduMapper();
    }
}




